package com.example.a16478091;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.ArrayList;

public class MenuActivity extends Activity {
    ArrayList<CheckBox> checkBoxes ;
    CheckBox cb_xcr, cb_hsdf, cb_djt, cb_xbc, cb_hsyk, cb_pjy;
    Food xcr, hsdf, djt, xbc, hsyk, pjy;
    EditText et_cheap;
    RadioGroup rg_cheap;
    RadioButton rb_cheap,rb_noCheap;
    double totalMoney = 0;
    Button btn_submit;
    Coupon coupon;
    String cheapMsg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        initView();
        initData();
        initEvent();
    }

    private void initData() {
        coupon = new Coupon("112200","钻石",0.75);
        cheapMsg = et_cheap.getText().toString();

        xcr = new Food("小炒肉",12);
        cb_xcr.setText(xcr.getFoodName()+"("+xcr.getFoodPrice()+"RMB)");

        hsdf = new Food("红烧豆腐",8);
        cb_hsdf.setText(hsdf.getFoodName()+"("+hsdf.getFoodPrice()+"RMB)");

        djt = new Food("大鸡腿",10);
        cb_djt.setText(djt.getFoodName()+"("+djt.getFoodPrice()+"RMB)");

        xbc = new Food("小白菜",8);
        cb_xbc.setText(xbc.getFoodName()+"("+xbc.getFoodPrice()+"RMB)");

        hsyk = new Food("红烧鱼块",15);
        cb_hsyk.setText(hsyk.getFoodName()+"("+hsyk.getFoodPrice()+"RMB)");

        pjy = new Food("啤酒鸭",12);
        cb_pjy.setText(pjy.getFoodName()+"("+pjy.getFoodPrice()+"RMB)");

    }

    private void initView() {
        cb_xcr = findViewById(R.id.cb_xcr);
        cb_hsdf = findViewById(R.id.cb_hsdf);
        cb_djt = findViewById(R.id.cb_djt);
        cb_xbc = findViewById(R.id.cb_xbc);
        cb_hsyk = findViewById(R.id.cb_hsyk);
        cb_pjy = findViewById(R.id.cb_pjy);
        et_cheap = findViewById(R.id.et_cheap);
        btn_submit = findViewById(R.id.btn_submit);
        rg_cheap = findViewById(R.id.rg_cheap);
        rb_cheap = findViewById(R.id.rb_cheap);
        rb_noCheap = findViewById(R.id.rb_noCheap);
    }

    private void initEvent() {
        rg_cheap.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_cheap:
                        et_cheap.setVisibility(View.VISIBLE);
                        et_cheap.setFocusableInTouchMode(true);
                        et_cheap.setFocusable(true);
                        et_cheap.requestFocus();
                        break;

                    case R.id.rb_noCheap:
                        et_cheap.setVisibility(View.GONE);
                        break;
                }
            }
        });

       btn_submit.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               if (cb_xcr.isChecked()){
                   totalMoney+=xcr.getFoodPrice();
               }if (cb_hsdf.isChecked()){
                   totalMoney+=hsdf.getFoodPrice();
               }if (cb_djt.isChecked()){
                   totalMoney+=djt.getFoodPrice();
               }if (cb_xbc.isChecked()){
                   totalMoney+=xbc.getFoodPrice();
               }if (cb_hsyk.isChecked()){
                   totalMoney+=hsyk.getFoodPrice();
               }if (cb_pjy.isChecked()){
                   totalMoney+=pjy.getFoodPrice();
               }
                if (cheapMsg.equals(coupon.number)){
                    Toast.makeText(getApplicationContext(),totalMoney*coupon.discount+"",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getApplicationContext(), totalMoney+ "", Toast.LENGTH_SHORT).show();
                }
               totalMoney=0;
           }
       });


    }
}
